﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Simple.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 Simple.BasicNet.Core.Net
 * 接口名称 ServiceConfigution
 * 开发人员：11920
 * 创建时间：2023/4/6 15:39:39
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace Simple.Framework.Configuration
{
    public class Configuration:IConfiguration
	{
		private ISimpleJobject simpleJobject;

		private static JObject jobject;


		private List<string> tokens;

		public static void SetConfiguration(string cfgPath)
		{
			if (!File.Exists(cfgPath))
			{
				throw new Exception("配置文件路径不存在!");
			}
			string json = File.ReadAllText(cfgPath);
			jobject = JsonConvert.DeserializeObject<JObject>(json);
		}
		static Configuration()
		{
			
			string cfgPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configuration.json");
			SetConfiguration(cfgPath);
		}

		public Configuration(ISimpleJobject simpleJobject)
		{
			this.simpleJobject=simpleJobject;
			tokens = new List<string>();
		}
   
        


        public IConfiguration GetSection(string key)
        {
			tokens.Add(key);
			return this;
        }

        public T GetValue<T>(string key)
        {
			tokens.Add(key);
		
			return GetValue<T>();
		}

		public T GetValue<T>(string [] keys)
		{
			if (!ToolSimpleCheck.IsNullOrEmpty(keys))
			{
				tokens.AddRange(keys);
			}
			return GetValue<T>();
		}

		public object GetValue(string[] keys,Type type)
		{
			if (!ToolSimpleCheck.IsNullOrEmpty(keys))
			{
				tokens.AddRange(keys);
			}

			return GetValue(type);
		}


		public T GetValue<T>()
		{
			if (ToolSimpleCheck.IsNullOrEmpty(tokens))
			{
				return default(T);
			}
			T t = simpleJobject.GetValue<T>(jobject, tokens.ToArray());
			tokens.Clear();
			return t;
		}

		public object GetValue(Type type)
		{
			if (ToolSimpleCheck.IsNullOrEmpty(tokens))
			{
				return null;
			}
			object o = simpleJobject.GetValue(jobject,type, tokens.ToArray());
			tokens.Clear();
			return o;
		}
	}
}
